package com.roin.learn.activiti.utils;

import com.alibaba.fastjson.JSON;
import com.roin.learn.activiti.formtype.RadioFormType;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.impl.form.DateFormType;
import org.activiti.engine.impl.form.EnumFormType;
import org.activiti.engine.impl.form.LongFormType;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 流程表单工具类
 */
public class ProcessFormUtil {
    /**
     * 将动态表单属性转成html
     * @param list
     * @return
     */
    public static String toHtml(List<FormProperty> list){
        if (list==null || list.isEmpty())
            return "";
        StringBuffer sb = new StringBuffer();
        for (FormProperty p : list) {
            if (p.getType() instanceof EnumFormType){
                if (p.getType() instanceof RadioFormType)
                    sb.append(getRadio(p));
                else
                  sb.append(getEnum(p));
            }else if (p.getType() instanceof DateFormType){
                sb.append(getDate(p));
            }else if (p.getType() instanceof LongFormType){
                sb.append(getLong(p));
            }else {
                sb.append(getString(p));
            }
        }
        return sb.toString();
    }

    private static StringBuffer getString(FormProperty p){
        StringBuffer sb = new StringBuffer();
        sb.append("<div class='layui-form-item' ");
        if (!p.isReadable())
            sb.append(" style='display:none;' ");
        sb.append("><label class='layui-form-label'>")
                .append(p.getName())
                .append("</label><div class='layui-input-block'><input type='text' name='")
                .append(p.getId())
                .append("' class='layui-input' ");
        if (StringUtils.isNotEmpty(p.getValue()))
            sb.append(" value='").append(p.getValue()).append("' ");

        if (!p.isWritable())
            sb.append(" readonly ");
        if (p.isRequired())
            sb.append("  lay-verify='required' ");
        sb.append("/></div></div>");
        return sb;
    }

    /**
     * 自定义的单选类型
     * @param p
     * @return
     */
    private static StringBuffer getRadio(FormProperty p){
        Map<String, String> values= (Map<String, String>) (p.getType().getInformation("values"));
        StringBuffer sb = new StringBuffer();
        sb.append("<div class='layui-form-item'");
        if (!p.isReadable())
            sb.append(" style='display:none;' ");
        sb.append("><label class='layui-form-label'>")
                .append(p.getName())
                .append("</label><div class='layui-input-block'>");
        for (Map.Entry<String, String> entry : values.entrySet()) {
            sb.append("<input type='radio' name='")
                    .append(p.getId())
                    .append("' value='")
                    .append(entry.getKey())
                    .append("' title='")
                    .append(entry.getValue())
                    .append("'");
            if (entry.getKey().equals(p.getValue()))
                sb.append(" checked");
            if (!p.isWritable())
                sb.append(" readonly ");
            if (p.isRequired())
                sb.append(" lay-verify='required' ");
        }
        /*for (String key : values.keySet()) {
            sb.append("<input type='radio' name='")
                    .append(p.getId())
                    .append("' value='")
                    .append(key)
                    .append("' title='")
                    .append(values.get(key))
                    .append("'");
            if (key.equals(p.getValue()))
                sb.append(" checked");
            if (!p.isWritable())
                sb.append(" readonly ");
            if (p.isRequired())
                sb.append(" lay-verify='required' ");
        }*/
        sb.append("/></div></div>");
        return sb;
    }

    private static StringBuffer getEnum(FormProperty p){
        Map<String, String> values= (Map<String, String>) (p.getType().getInformation("values"));
        StringBuffer sb = new StringBuffer();
        sb.append("<div class='layui-form-item'");
        if (!p.isReadable())
            sb.append(" style='display:none;' ");
        sb.append("><label class='layui-form-label'>")
                .append(p.getName())
                .append("</label><div class='layui-input-block'> <select name='")
                .append(p.getId())
                .append("' ");
        if (StringUtils.isNotEmpty(p.getValue()))
            sb.append(" value='").append(p.getValue()).append("' ");
        if (!p.isWritable())
            sb.append(" readonly ");
        if (p.isRequired())
            sb.append("  lay-verify='required' >");
        sb.append("<option value=''>--请选择--</option>");
        for (Map.Entry<String, String> entry : values.entrySet()) {
            sb.append("<option value='").append(entry.getKey()).append("'>").append(entry.getValue()).append("</option>");
        }
        /*for (String key : values.keySet()) {
            sb.append("<option value='").append(key).append("'>").append(values.get(key)).append("</option>");
        }*/
        sb.append("</select></div></div>");
        return sb;
    }
    private static StringBuffer getDate(FormProperty p){
        StringBuffer sb = new StringBuffer();
        sb.append("<div class='layui-form-item'");
        if (!p.isReadable())
            sb.append(" style='display:none;' ");
        sb.append("><label class='layui-form-label'>")
                .append(p.getName())
                .append("</label><div class='layui-input-block'><input type='text'  name='")
                .append(p.getId())
                .append("' class='layui-input date' ");
        if (StringUtils.isNotEmpty(p.getValue()))
            sb.append(" value='").append(p.getValue()).append("' ");
        if (!p.isWritable())
            sb.append(" readonly ");
        if (p.isRequired())
            sb.append("  lay-verify='required' ");
        sb.append("/></div></div>");
        return sb;
    }
    private static StringBuffer getLong(FormProperty p){
        StringBuffer sb = new StringBuffer();
        sb.append("<div class='layui-form-item'");
        if (!p.isReadable())
            sb.append(" style='display:none;' ");
        sb.append("><label class='layui-form-label'>")
                .append(p.getName())
                .append("</label><div class='layui-input-block'><input type='number' min='0.5' max='60' name='")
                .append(p.getId())
                .append("' class='layui-input' ");
        if (StringUtils.isNotEmpty(p.getValue()))
            sb.append(" value='").append(p.getValue()).append("' ");
        if (!p.isWritable())
            sb.append(" readonly ");
        if (p.isRequired())
            sb.append("  lay-verify='required' ");
        sb.append("/></div></div>");
        return sb;
    }
}
